package com.xiaomo.gitee.datamanager.controller.admin;

import com.xiaomo.gitee.datamanager.common.result.Code;
import com.xiaomo.gitee.datamanager.common.result.Result;
import com.xiaomo.gitee.datamanager.service.image.ImageService;
import com.xiaomo.gitee.datamanager.service.temp.PageService;
import com.xiaomo.gitee.datamanager.service.image.impl.ImageServiceImpl;
import com.xiaomo.gitee.datamanager.service.temp.PageServiceImpl;
import com.xiaomo.gitee.datamanager.util.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.multipart.MultipartFile;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

@RestController
@RequestMapping("/admin/api")
@Tag(name = "图片操作", description = "上传和删除图片")
@RequiredArgsConstructor
public class AdminImageController {

    private final ImageService imageService;
    private final PageService pageService;
    private final Executor threadPool;


    @PutMapping("/image")
    @PreAuthorize("hasAuthority('image_cd')")
    @Operation(
            summary = "上传图片",
            description = "上传单个或多个图片",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "category",
                            description = "大类别ID",
                            required = true
                    ),
                    @Parameter(
                            name = "sub-category",
                            description = "数据集ID",
                            required = true
                    ),
                    @Parameter(
                            name = "description",
                            description = "图片描述"
                    ),
                    @Parameter(
                            name = "image",
                            description = "图片文件",
                            required = true
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> uploadImage(@RequestParam("category") Long category,
                                 @RequestParam("sub-category") Long subCategory,
                                 @RequestParam("description") String description,
                                 @RequestParam("image") MultipartFile[] imageFiles) {

        imageService.uploadImage(category, subCategory, description, imageFiles);
        //TODO 断点重连
        return Result.ok();
    }

    @DeleteMapping("/image/{id}")
    @PreAuthorize("hasAuthority('image_cd')")
    @Operation(
            summary = "删除图片",
            description = "通过图片id删除图片",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "id",
                            description = "图片ID,类型为UUID",
                            required = true,
                            in = ParameterIn.PATH
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    schema = @Schema(
                                            implementation = Result.class
                                    )
                            )
                    )
            }
    )
    public Result<?> deleteImage(@PathVariable String id) {
        imageService.deleteImage(id);
        return Result.ok();
    }

}
